home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include <string.h>
- #include "comm.h"
- #include <unistd.h>
- extern "C" {
- #include <sys/ioctl.h>
- }
- #include <sys/socket.h>
- #include <stdio.h>
- #include <errno.h>
- #include <bstring.h>
- #include <netdir.h>
-
- //
- // NetId
- //
-
- NetId::NetId()
- {
- id.s_addr = INADDR_ANY;
- }
-
- NetId::NetId(const char* a)
- {
- memcpy(&id.s_addr, a, sizeof(id));
- }
-
- NetId::NetId(InAddr& a)
- {
- id = a;
- }
-
- NetId::NetId(InAddr* a)
- {
- id = *a;
- }
-
- NetId::operator InAddr () const
- {
- return id;
- }
-
- int NetId::operator == (const NetId& n) const
- {
- if (isAny()) return TRUE;
- return (memcmp(&id, &n.id, sizeof(id)) == 0);
- }
-
- int NetId::operator != (const NetId& n) const
- {
- if (isAny()) return FALSE;
- return (memcmp(&id, &n.id, sizeof(id)) != 0);
- }
-
- int NetId::isAny() const
- {
- return (id.s_addr == INADDR_ANY);
- }
-
- //
- // Broadcast socket stuff
- //
-
- int openBroadcastSocket(int port, struct sockaddr_in* addr)
- {
- // Open the socket
- int fd = socket(AF_INET, SOCK_DGRAM, 0);
- if (fd < 0) {
- perror("making broadcast socket");
- return -1;
- }
-
- // bind it to the desired port
- bzero(addr, sizeof(*addr));
- addr->sin_addr.s_addr = INADDR_ANY;
- addr->sin_family = AF_INET;
- addr->sin_port = port;
- if (bind(fd, addr, sizeof(*addr)) < 0) {
- perror("bind");
- close(fd);
- return -1;
- }
-
- // make it a broadcast socket
- int on = 1;
- if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) {
- perror("setsockopt");
- close(fd);
- return -1;
- }
- addr->sin_addr.s_addr = INADDR_BROADCAST;
-
- // make it non-blocking
- if (ioctl(fd, FIONBIO, &on) < 0) {
- perror("ioctl");
- close(fd);
- return -1;
- }
-
- return fd;
- }
-